﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author YUga studio
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.invoicing.service;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.invoicing.entity.AccountDetail;
import com.oplink.webbase.modules.invoicing.dao.AccountDetailDao;



/**
 * 告警Service
 * @author YUga studio
 * @version 2015-01-12
 */
@Component
@Transactional
public class AccountDetailService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(AccountDetailService.class);
	
	@Autowired
	private AccountDetailDao accountDetailDao;
	
	public AccountDetail get(Long id) {
		return accountDetailDao.findOne(id);
	}
	
	public Page<AccountDetail> find(Page<AccountDetail> page, AccountDetail accountDetail) {
		DetachedCriteria dc = accountDetailDao.createDetachedCriteria();
		//添加查询条件
		if (null != accountDetail.getReceiveId()
				&& StringUtils.isNotEmpty(accountDetail.getReceiveId().toString())) {
			dc.add(Restrictions.like("receiveId", "%"+accountDetail.getReceiveId()+"%"));
		}
		if (null != accountDetail.getSupplierId()
				&& StringUtils.isNotEmpty(accountDetail.getSupplierId().toString())) {
			dc.add(Restrictions.like("supplierId", "%"+accountDetail.getSupplierId()+"%"));
		}
		if (null != accountDetail.getPaidOwner()
				&& StringUtils.isNotEmpty(accountDetail.getPaidOwner().toString())) {
			dc.add(Restrictions.like("paidOwner", "%"+accountDetail.getPaidOwner()+"%"));
		}
		dc.add(Restrictions.eq(AccountDetail.DEL_FLAG, AccountDetail.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return accountDetailDao.find(page, dc);
	}
	
	/**
	  * @function 创建查询
	  **/
	public Page<AccountDetail> getUserAccountDetail(Page<AccountDetail> page, AccountDetail accountDetail) {
		DetachedCriteria dc = accountDetailDao.createDetachedCriteria();
		//添加查询条件
		if (null != accountDetail.getReceiveId()
				&& StringUtils.isNotEmpty(accountDetail.getReceiveId().toString())) {
			dc.add(Restrictions.like("receiveId", "%"+accountDetail.getReceiveId()+"%"));
		}
		if (null != accountDetail.getSupplierId()
				&& StringUtils.isNotEmpty(accountDetail.getSupplierId().toString())) {
			dc.add(Restrictions.like("supplierId", "%"+accountDetail.getSupplierId()+"%"));
		}
		if (null != accountDetail.getPaidOwner()
				&& StringUtils.isNotEmpty(accountDetail.getPaidOwner().toString())) {
			dc.add(Restrictions.like("paidOwner", "%"+accountDetail.getPaidOwner()+"%"));
		}
		dc.add(Restrictions.eq(AccountDetail.DEL_FLAG, AccountDetail.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return accountDetailDao.find(page, dc);
	}
	
	public AccountDetail getAccountDetail(Long id) {
		return accountDetailDao.findOne(id);
	}

	public void saveAccountDetail(AccountDetail entity) {
		accountDetailDao.save(entity);
	}
	
	public void deleteAccountDetail(Long id) {
		accountDetailDao.delete(id);
	}
	
	@Autowired
	public void setAccountDetailDao(AccountDetailDao accountDetailDao) {
		this.accountDetailDao = accountDetailDao;
	}	
}
